<?php
/**
 * 生成HTML脚本
 * author:iceboy(QQ:1103318)
 * date:2009-03-17
 */
class html{
	#生成按钮
	static function input($value,$type='button',$attribute=array()){
		$return=<<<ICEBOY
			<input type="$type" value="$value" 
ICEBOY;
		foreach ($attribute as $k=>$v){
			$return.=<<<ICEBOY
								 $k = "$v" 
ICEBOY;
		}
		$return.='/ >';
		return $return;					
	}
	#生成DIV
	static function div($html,$class=null,$attribute=array()){
		$return=<<<ICEBOY
			<div class="$class" 
ICEBOY;
		if (!empty($class)){
			$return.=<<<ICEBOY
				class="$class" 
ICEBOY;
		}
		foreach ($attribute as $k=>$v){
			$return.=<<<ICEBOY
								 $k = "$v" 
ICEBOY;
		}
		$return.=<<<ICEBOY
								 >$html</div> 
ICEBOY;
		return $return;
	}
	#生成LI
	static function li($html,$class=null,$attribute=array()){
		$return=<<<ICEBOY
			<li
ICEBOY;
		if (!empty($class)){
			$return.=<<<ICEBOY
				class="$class" 
ICEBOY;
		}
		foreach ($attribute as $k=>$v){
			$return.=<<<ICEBOY
								 $k = "$v" 
ICEBOY;
		}
		$return.=<<<ICEBOY
								 >$html</li> 
ICEBOY;
		return $return;
	}
	#生成img
	static function img($src,$attribute=array()){
		$return=<<<ICEBOY
			<img src="$src"
ICEBOY;
		foreach($attribute as $k=>$v){
			$return.=<<<ICEBOY
				$k = "$v" 
ICEBOY;
		}
		$return.=<<<ICEBOY
								 />
ICEBOY;
		return $return;
	}
	#生成a
	static function a($href,$text,$attribute=array()){
		$return=<<<ICEBOY
			<a href="$href"
ICEBOY;
		foreach($attribute as $k=>$v){
			$return.=<<<ICEBOY
				$k = "$v" 
ICEBOY;
		}
		$return.=<<<ICEBOY
								 >$text</a>
ICEBOY;
		return $return;
	}
	#生成selectbox
	static function selectbox(array $data,$value,$attribute=array()){
		$return='<select ';
		foreach ($attribute as $k=>$v){
			$return.=<<<ICEBOY
				$k = "$v" 
ICEBOY;
		}
		$return.=<<<ICEBOY
			>
ICEBOY;
		foreach ($data as $k=>$v){
			$return.=<<<ICEBOY
				<option value="$v" 
ICEBOY;
			if ($value==$v){
				$return.=<<<ICEBOY
					selected 
ICEBOY;
			}
			$return.=<<<ICEBOY
				>$k</option>
ICEBOY;
		}
		$return.=<<<ICEBOY
			</select>
ICEBOY;
		return $return;
	}
	#生成table
	static function table($html,$width='100%',$attrbute=array()){
		if(!is_array($html)){
			return false;
		}
		$return=<<<ICEBOY
			<table width="$width"
ICEBOY;
		foreach ($attrbute as $k=>$v){
			$return.=<<<ICEBOY
				$k = "$v" 
ICEBOY;
		}
		$return.=<<<ICEBOY
			>
ICEBOY;
		foreach ($html as $v){
			$return.=<<<ICEBOY
				<$v[flag]
ICEBOY;
			if (!empty($v['attr']) && is_array($v['attr'])) {
				foreach ($v['attr'] as $k2=>$v2){
					$return.=<<<ICEBOY
						$k2 = "$v2" 
ICEBOY;
				}
			}
			$return.=<<<ICEBOY
				>
ICEBOY;
			foreach ($v['html'] as $k1=>$v1){
				$return.=<<<ICEBOY
					<td>$v1</td>
ICEBOY;
			}
			$return.=<<<ICEBOY
				</$v[flag]>
ICEBOY;
		}
		$return.=<<<ICEBOY
			</table>
ICEBOY;
		return $return;
	}
	/**
	 *	生成groupselect 
	 * parameter:
	 * 		$group是组名，如果其下有小项：其中key是分类别，value是分类ID，对应$data里的key值来区分小项，如果没有小项就直接为小项
	 * 		$data是小项。
	 * 		还得修改。。。。。。先用着吧
	 */
	
	function selectgroup(array $group,array $data,$value,$attribute=array()){
		$return='<select ';
		foreach ($attribute as $k=>$v){
			$return.=<<<ICEBOY
				$k = "$v" 
ICEBOY;
		}
		$return.=<<<ICEBOY
			>
ICEBOY;
		foreach ($group as $k=>$v){
			if (empty($data[$v])) {
				$return.=<<<ICEBOY
						<option value="$v" 
ICEBOY;
					if ($value==$v){
						$return.=<<<ICEBOY
							selected 
ICEBOY;
					}
					$return.=<<<ICEBOY
						>$k</option>
ICEBOY;
			}else{
				$return.=<<<ICEBOY
						<optgroup label="$k">
ICEBOY;
				foreach ($data[$v] as $v1){
					$return.=<<<ICEBOY
						<option value="$v1[id]" 
ICEBOY;
					if ($value==$v1['id']){
						$return.=<<<ICEBOY
							selected 
ICEBOY;
					}
					$return.=<<<ICEBOY
						>$v1[name]</option>
ICEBOY;
				}
					$return.=<<<ICEBOY
						</optgroup>
ICEBOY;
			}
		}
		$return.=<<<ICEBOY
			</select>
ICEBOY;
		return $return;
	}
	/**
	 * author:iceboy
	 * date:2009-03-28
	 * module:生成单选框
	 * parameter:
	 * 	$radio是其值，其中key是元素单选框的值，value是单选框对应的显示值
	 * 	$check_value是选中框的值
	 * 	$name是单选框的名字
	 */
	function radio(array $radio,$name,$check_value,$attribute=array()){
		$return='';
		$attr='';
		foreach ($attribute as $key=>$value){
			$attr.=<<<ICEBOY
				$k = "$v" 
ICEBOY;
		}
		foreach ($radio as $key=>$value){
			$return.=<<<ICEBOY
			<input type="radio" name="$name" value="$key" $attr 
ICEBOY;
			if ($key==$check_value) {
				$return.=' checked ';
			}
			$return.=<<<ICEBOY
			>$value
ICEBOY;
		}
		return $return;
	}
	#生成LI
	static function span($html,$class=null,$attribute=array()){
		$return=<<<ICEBOY
			<span
ICEBOY;
		if (!empty($class)){
			$return.=<<<ICEBOY
				class="$class" 
ICEBOY;
		}
		foreach ($attribute as $k=>$v){
			$return.=<<<ICEBOY
								 $k = "$v" 
ICEBOY;
		}
		$return.=<<<ICEBOY
								 >$html</span> 
ICEBOY;
		return $return;
	}
}
?>